package com.longrise.co.ui.carfiles.presenter;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.util.EnCryptDeCrypt;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.carfiles.contract.CarFilesContract;
import com.longrise.co.ui.carfiles.model.ComplaintBean;
import com.longrise.co.ui.carfiles.model.EvaluationBean;
import com.longrise.co.ui.carfiles.model.RepairRecord;
import com.longrise.co.ui.carfiles.model.RepairRecordDetail;
import com.longrise.co.ui.carfiles.model.UploadBean;
import com.longrise.co.ui.home.model.BindCarInfo;
import com.longrise.co.ui.home.model.CompanyDetail;
import com.longrise.co.ui.home.model.HomeModel;
import com.ly.common.baserx.RxSubscriber;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.security.EnCryptDeCrypt2;

/**
 * Created by luoyang on 2017-6-23.
 */

public class CarFilesPresenter extends CarFilesContract.Presenter {

    private HomeModel homeModel = new HomeModel();

    @Override
    public void getBindCarList(String carNo, boolean isShowProcess) {
        try {
            EntityBean bean = new EntityBean();

            if(!StringUtils.isEmpty(carNo)) {
                bean.set("carno", carNo);
            }
            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("sign", CommonPara.getInstance().getEnUserFlag());
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(homeModel.bindCarListRequest(bean).subscribe(new RxSubscriber<BindCarInfo>(mContext, isShowProcess) {
                @Override
                protected void _onNext(BindCarInfo bean) {
                   mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getRepairRecordList(String carNo, String vin, String pageNum, boolean isShowProcess) {
        try {
            EntityBean bean = new EntityBean();

            if(StringUtils.isEmpty(carNo)) {
                mView.showErrorTip("车牌号不能为空");
                return ;
            }
            String enCarNo = EnCryptDeCrypt2.getInstance().EnCrypt(carNo);
            bean.set("carno", carNo);
            bean.set("carvin", vin);
            bean.set("sign", enCarNo);
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(mModel.repairRecordRequest(bean).subscribe(new RxSubscriber<RepairRecord>(mContext, isShowProcess) {
                @Override
                protected void _onNext(RepairRecord bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getCompanyDetail(String companyId) {
        try {
            if(StringUtils.isEmpty(companyId)) {
                mView.showErrorTip("companyId不能为空");
                return ;
            }
            EntityBean bean = new EntityBean();

            String enCompanyId = EnCryptDeCrypt2.getInstance().EnCrypt(companyId);
            bean.set("companyid", companyId);
            bean.set("areaid", CommonPara.getInstance().getAreaId());
            bean.set("sign", enCompanyId);
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(homeModel.companyDetailRequest(bean).subscribe(new RxSubscriber<CompanyDetail>(mContext, true) {
                @Override
                protected void _onNext(CompanyDetail bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void uploadPhoto(String imageInfo, String type) {
        try {
            if(StringUtils.isEmpty(imageInfo) || StringUtils.isEmpty(type)) {
                mView.showErrorTip("上传照片失败，参数有误");
                return ;
            }
            EntityBean bean = new EntityBean();

            bean.set("imageInfo", imageInfo);
            bean.set("type", type);

            mRxManage.add(mModel.uploadPhotoRequest(bean).subscribe(new RxSubscriber<UploadBean>(mContext, true) {
                @Override
                protected void _onNext(UploadBean bean) {
                    if(mView instanceof CarFilesContract.ComplaintView) {
                        ((CarFilesContract.ComplaintView) mView).uploadPhotoSuccess(bean);
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("上传图片失败");
        }
    }

    @Override
    public void carComplaint(String recordId, String year, String complaintDetails, String complaintImg) {
        try {
            if(StringUtils.isEmpty(recordId) || StringUtils.isEmpty(year) || StringUtils.isEmpty(complaintDetails)){
                mView.showErrorTip("投诉失败，参数有误");
                return ;
            }
            EntityBean bean = new EntityBean();
            String enRecordId = EnCryptDeCrypt2.getInstance().EnCrypt(recordId);

            bean.set("recordid", recordId);
            bean.set("year", year);
            bean.set("complaintdetails", complaintDetails);
            if(!StringUtils.isEmpty(complaintImg) && !"]".equals(complaintImg)) {
                bean.set("complaintimg", complaintImg);
            }
            bean.set("sign", enRecordId);

            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("areaid", CommonPara.getInstance().getAreaId());
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(mModel.carComplaintRequest(bean).subscribe(new RxSubscriber<ComplaintBean>(mContext, true) {
                @Override
                protected void _onNext(ComplaintBean bean) {
                    if(mView instanceof CarFilesContract.ComplaintView) {
                        ((CarFilesContract.ComplaintView) mView).submitSuccess();
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void carEvaluation(EntityBean bean) {
        try {
            mRxManage.add(mModel.carEvaluationRequest(bean).subscribe(new RxSubscriber<EvaluationBean>(mContext, true) {
                @Override
                protected void _onNext(EvaluationBean bean) {
                    if(mView instanceof CarFilesContract.ComplaintView) {
                        ((CarFilesContract.ComplaintView) mView).submitSuccess();
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void repairRecordDetailRequest(String recordId, String year, String carNo) {
        try {
            if(StringUtils.isEmpty(recordId) || StringUtils.isEmpty(year) || StringUtils.isEmpty(carNo)){
                mView.showErrorTip("查询失败，参数有误");
                return ;
            }
            EntityBean bean = new EntityBean();
            String enRecordId = EnCryptDeCrypt2.getInstance().EnCrypt(recordId);
            String log = EnCryptDeCrypt2.getInstance().EnCrypt("120.1617445782");
            bean.set("recordid", recordId);
            bean.set("year", year);
            bean.set("carno", carNo);
            bean.set("sign", enRecordId);

            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(mModel.repairRecordDetailRequest(bean).subscribe(new RxSubscriber<RepairRecordDetail>(mContext, true) {
                @Override
                protected void _onNext(RepairRecordDetail bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }
}
